/**
 * Created by 55456 on 2016/4/10.
 */
var db = require("../../models/index");
var codeConfig = require("../../configs/codeConfig");
var appConfig  = require("../../configs/appConfig");
var auth = require("../../helpers/auth");
var userAdaptor = require("../../modelsAdaptor/userAdaptor");
var autoPrefix = appConfig.autoPrefix;


module.exports = (router) => {
    router.post(autoPrefix("userinfo"),function *(){
        this.checkBody("AuthorID").notEmpty();
        if(this.errors){
            this.body = codeConfig.parseErrorMsg(this.errors);
            return;
        }
        var body = this.request.body;
        var isOwn = body.AuthorID === (yield auth.user(this)).id;
        var userinfo = yield userAdaptor.getUserInfo(body.AuthorID,true);
        this.body = codeConfig.parseMsg(userinfo);
    });

    router.post(autoPrefix("userinfo/update"),function *(){
        this.checkBody("userName").notEmpty();
        this.checkBody("sign").notEmpty();
        this.checkBody("sex").notEmpty().toInt();
        this.checkBody("avatar").isUrl("error url pattern");
        this.checkBody("noticeEnable").notEmpty().toInt();
        this.checkBody("followingEnable").notEmpty().toInt();
        this.checkBody("followerEnable").notEmpty().toInt();
        this.checkBody("aboutNotice").notEmpty().toInt();
        this.checkBody("updateNotice").notEmpty().toInt();
        if(this.errors){
            this.body = codeConfig.parseErrorMsg(this.errors);
            return;
        }
        var body = this.request.body;
        var userID = (yield auth.user(this)).id;
        var userinfo = yield userAdaptor.updateUserInfo(userID,body);
        this.body = codeConfig.parseMsg(userinfo);
    });

    router.post(autoPrefix("userinfo/addfollow"),function *(){
        this.checkBody("starID").notEmpty();
        if(this.errors){
            this.body = codeConfig.parseErrorMsg(this.errors);
            return;
        }
        var body = this.request.body;
        var userID = (yield auth.user(this)).id;
        var msg = yield userAdaptor.addStar(userID,body.starID);
        this.body = codeConfig.parseMsg(msg);
    });

    router.post(autoPrefix("userinfo/delfollow"),function *(){
        this.checkBody("starID").notEmpty();
        if(this.errors){
            this.body = codeConfig.parseErrorMsg(this.errors);
            return;
        }
        var body = this.request.body;
        var userID = (yield auth.user(this)).id;
        var msg = yield userAdaptor.delStar(userID,body.starID);
        this.body = codeConfig.parseMsg(msg);
    });
};
